﻿using System;
using System.Collections.Generic;
using System.Text;

namespace XP.Utilities.Utility
{
    public class UnitConvert
    {
        /// <summary>
        /// 转换单位
        /// </summary>
        public enum Unit
        {
            /// <summary>
            /// 像素
            /// </summary>
            Pixel,
            /// <summary>
            /// 英尺
            /// </summary>
            Inch,
            /// <summary>
            /// 毫米
            /// </summary>
            Millimeter
        }

        private Unit _unit;
        private decimal _value;
        private Dictionary<Unit, Dictionary<Unit, decimal>> keys;

        public UnitConvert()
        {
            keys = new Dictionary<Unit, Dictionary<Unit, decimal>>();

            keys.Add(Unit.Pixel, new Dictionary<Unit, decimal>());
            keys[Unit.Pixel].Add(Unit.Inch, (decimal)0.014);
            keys[Unit.Pixel].Add(Unit.Millimeter, (decimal)0.264);

            keys.Add(Unit.Millimeter, new Dictionary<Unit, decimal>());
            keys[Unit.Millimeter].Add(Unit.Inch, (decimal)0.039);
            keys[Unit.Millimeter].Add(Unit.Pixel, (decimal)3.78);

            keys.Add(Unit.Inch, new Dictionary<Unit, decimal>());
            keys[Unit.Inch].Add(Unit.Millimeter, (decimal)25.4);
            keys[Unit.Inch].Add(Unit.Pixel, 72);
        }

        public UnitConvert(decimal value, Unit unit)
            : this()
        {
            _unit = unit;
            _value = value;
        }

        public UnitConvert(Unit unit)
            : this(0, unit)
        {
        }

        public decimal Convert(Unit convertUnit)
        {
            return keys[_unit][convertUnit] * _value;
        }
        public decimal Convert(decimal length, Unit convertUnit)
        {
            decimal value = keys[_unit][convertUnit] * length;
            return value;
        }
        //public static double MillimetersToPixels(double length)
        //{
        //    System.Windows.Forms.Panel p = new System.Windows.Forms.Panel();
        //    System.Drawing.Graphics g = System.Drawing.Graphics.FromHwnd(p.Handle);
        //    IntPtr hdc = g.GetHdc();
        //    int width = Win32.GDI.GetDeviceCaps(hdc, 4);     // HORZRES 
        //    int pixels = Win32.GDI.GetDeviceCaps(hdc, 8);     // BITSPIXEL
        //    g.ReleaseHdc(hdc);
        //    return (((double)pixels / (double)width) * (double)length);
        //}
    }
}
